//
//  DDNetworkTool.swift
//  doudou_weibo
//
//  Created by apple on 2016/11/15.
//  Copyright © 2016年 apple. All rights reserved.
//

import UIKit
import AFNetworking


//定义请求类型的枚举
enum HttpMethod : Int {
    case GET = 0
    case POST
}


class DDNetworkTool: AFHTTPSessionManager {
    
    //单例对象
    static let sharedManager: DDNetworkTool = {
        
        let tool = DDNetworkTool()
        
        tool.responseSerializer.acceptableContentTypes?.insert("text/html")
        
        return tool
        
    }()
    
    //网络请求的封装方法
    func request(Method: HttpMethod, urlString: String, parameters: Any?, finishClosure: @escaping (Any?,Error?) -> ()) {
        
        if Method == .GET {
            
            self.get(urlString, parameters: parameters, progress: nil, success: { (task:URLSessionDataTask, responsedObject: Any) in
                finishClosure(responsedObject,nil)
            }, failure: { (task: URLSessionDataTask?, error: Error) in
                finishClosure(nil,error)
            })
        }else{
            self.post(urlString, parameters: parameters, progress: nil, success: { (task:URLSessionDataTask, responsedObject: Any) in
                finishClosure(responsedObject,nil)
            }, failure: { (task: URLSessionDataTask?, error: Error) in
                finishClosure(nil,error)
            })
        }
    
    }
    

}
